package org.springframework.orm.hibernate3;

import java.util.Iterator;

import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.PersistentClass;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;

import cn.desk.commons.util.HibernateSessionFactoryRebuild;
import cn.desk.commons.util.ListAddValueInjectionInvoker;

public class HibenatePropertyListAddValueInjectionInvoker extends
		ListAddValueInjectionInvoker implements HibernateSessionFactoryRebuild {
	private String sessionFactory;

	@Override
	public void invoke(Object target) {
		super.invoke(target);
		rebuildFactory();
	}

	public void rebuildFactory() {
		AnnotationSessionFactoryBean factory = (AnnotationSessionFactoryBean) context
				.getBean(sessionFactory);
		try {
			Configuration config = factory.getConfiguration();
			Iterator<PersistentClass> ite = config.getClassMappings();
			while (ite.hasNext()) {
				System.out.println(ite.next());
			}
			String name;
			for (Object l : target) {
				name = String.valueOf(l);
				if (name.endsWith("xml")) {
					config.addFile(name);
				} else {
					config.addAnnotatedClass(Class.forName(l.toString()));
				}
			}
//			config.buildSessionFactory();
			factory.buildSessionFactory();
			 ite = config.getClassMappings();
				while (ite.hasNext()) {
					System.out.println(ite.next());
				}
//			factory.buildSessionFactory();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(String sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
}
